

package Cliente;

import callProcs.ProcsCliente;
import java.sql.SQLException;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableColumnModel;
import javax.swing.table.DefaultTableModel;


public class BusquedaCliente extends javax.swing.JFrame {

    
    public java.sql.ResultSet rs ;
    public String consulta = new String();
    public ProcsCliente verCliente = new ProcsCliente();
    public DefaultTableModel nombres = new DefaultTableModel();
    public DefaultTableColumnModel columnas = new DefaultTableColumnModel(); 
       
    /** Creates new form NewJFrame */
    public BusquedaCliente() {
        initComponents();
       // tCliente = new JTable(nombres,columnas);
    }

    
    
    public void DescribeTabla(){
        
     nombres.addColumn("IdCliente");
     nombres.addColumn("Nombre");
     nombres.addColumn("No. Cedula");
     nombres.addColumn("No. DPI");
     nombres.addColumn("Dirección");
     nombres.addColumn("Telefono ");
     
     columnas.getColumn(0).setMaxWidth(5);
     columnas.getColumn(1).setMinWidth(200);
     columnas.getColumn(2).setMinWidth(80);
     columnas.getColumn(3).setMinWidth(100);
     columnas.getColumn(4).setMinWidth(200);
     columnas.getColumn(5).setMinWidth(80);
     //ocultando col 1
     columnas.getColumn(0).setMaxWidth(0);
     columnas.getColumn(0).setMinWidth(0);
     columnas.getColumn(0).setPreferredWidth(0);
     
     tCliente.getTableHeader().getColumnModel().getColumn(0).setMaxWidth(0);
     tCliente.getTableHeader().getColumnModel().getColumn(0).setMaxWidth(0);
        
        
    }
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jPanel1 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        rNombre = new javax.swing.JRadioButton();
        rCedula = new javax.swing.JRadioButton();
        rDpi = new javax.swing.JRadioButton();
        tBusqueda = new javax.swing.JTextField();
        jPanel2 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tCliente = new javax.swing.JTable();
        jLabel3 = new javax.swing.JLabel();
        Beditar = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(bufete.BufeteApp.class).getContext().getResourceMap(BusquedaCliente.class);
        setTitle(resourceMap.getString("Form.title")); // NOI18N
        setName("Form"); // NOI18N

        jLabel1.setFont(resourceMap.getFont("jLabel1.font")); // NOI18N
        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N

        jPanel1.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
        jPanel1.setName("jPanel1"); // NOI18N

        jLabel2.setFont(resourceMap.getFont("jLabel2.font")); // NOI18N
        jLabel2.setText(resourceMap.getString("jLabel2.text")); // NOI18N
        jLabel2.setName("jLabel2"); // NOI18N

        rNombre.setText(resourceMap.getString("rNombre.text")); // NOI18N
        rNombre.setName("rNombre"); // NOI18N
        rNombre.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                rNombreActionPerformed(evt);
            }
        });

        rCedula.setText(resourceMap.getString("rCedula.text")); // NOI18N
        rCedula.setName("rCedula"); // NOI18N
        rCedula.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                rCedulaActionPerformed(evt);
            }
        });

        rDpi.setText(resourceMap.getString("rDpi.text")); // NOI18N
        rDpi.setName("rDpi"); // NOI18N
        rDpi.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                rDpiActionPerformed(evt);
            }
        });

        tBusqueda.setText(resourceMap.getString("tBusqueda.text")); // NOI18N
        tBusqueda.setEnabled(false);
        tBusqueda.setName("tBusqueda"); // NOI18N
        tBusqueda.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                tBusquedaKeyReleased(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(24, 24, 24)
                .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 106, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(36, 36, 36)
                .addComponent(rNombre)
                .addGap(68, 68, 68)
                .addComponent(rCedula)
                .addGap(69, 69, 69)
                .addComponent(rDpi)
                .addContainerGap(196, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap(114, Short.MAX_VALUE)
                .addComponent(tBusqueda, javax.swing.GroupLayout.PREFERRED_SIZE, 459, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(179, 179, 179))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(rNombre)
                    .addComponent(rDpi)
                    .addComponent(rCedula))
                .addGap(18, 18, 18)
                .addComponent(tBusqueda, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(39, Short.MAX_VALUE))
        );

        jPanel2.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
        jPanel2.setName("jPanel2"); // NOI18N

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        tCliente.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null}
            },
            new String [] {
                "Nombre Completo", "No. Cedula", "No. DPI", "Dirección", "No. Telefono"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false, false, false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        tCliente.setName("tCliente"); // NOI18N
        jScrollPane1.setViewportView(tCliente);
        tCliente.getColumnModel().getColumn(0).setHeaderValue(resourceMap.getString("tCliente.columnModel.title0")); // NOI18N
        tCliente.getColumnModel().getColumn(1).setHeaderValue(resourceMap.getString("tCliente.columnModel.title1")); // NOI18N
        tCliente.getColumnModel().getColumn(2).setHeaderValue(resourceMap.getString("tCliente.columnModel.title2")); // NOI18N
        tCliente.getColumnModel().getColumn(3).setHeaderValue(resourceMap.getString("tCliente.columnModel.title3")); // NOI18N
        tCliente.getColumnModel().getColumn(4).setHeaderValue(resourceMap.getString("tCliente.columnModel.title4")); // NOI18N

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 732, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 275, Short.MAX_VALUE)
                .addContainerGap())
        );

        jLabel3.setIcon(resourceMap.getIcon("jLabel3.icon")); // NOI18N
        jLabel3.setText(resourceMap.getString("jLabel3.text")); // NOI18N
        jLabel3.setName("jLabel3"); // NOI18N

        Beditar.setIcon(resourceMap.getIcon("Beditar.icon")); // NOI18N
        Beditar.setText(resourceMap.getString("Beditar.text")); // NOI18N
        Beditar.setName("Beditar"); // NOI18N
        Beditar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BeditarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(273, 273, 273)
                .addComponent(jLabel1)
                .addGap(19, 19, 19))
            .addGroup(layout.createSequentialGroup()
                .addGap(42, 42, 42)
                .addComponent(jLabel3)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 453, Short.MAX_VALUE)
                .addComponent(Beditar)
                .addGap(18, 18, 18))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addGap(38, 38, 38)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(31, 31, 31)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel3)
                    .addComponent(Beditar))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void tBusquedaKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_tBusquedaKeyReleased
    Vector<String> tableHeaders = new Vector<String>();
    Vector tableData = new Vector();
    tableHeaders.add("IdCliente");
    tableHeaders.add("Nombre"); 
    tableHeaders.add("No. Cedula");
    tableHeaders.add("No. DPI");
    tableHeaders.add("Direccion");
    tableHeaders.add("Telefono");
       
        try {
          rs = verCliente.VerdatosCliente(consulta+tBusqueda.getText()+"%'");
          while (rs.next()){
             
        Vector<Object> oneRow = new Vector<Object>();
        oneRow.add(rs.getInt("idCliente"));
        oneRow.add(rs.getString("Nombre"));
        oneRow.add(rs.getString("No. Cedula"));
        oneRow.add(rs.getString("Dpi_cliente"));
        oneRow.add(rs.getString("Direccion"));
      //  oneRow.add(rs.getObject("idDireccion"));
        oneRow.add(rs.getString("Telefono"));
        tableData.add(oneRow);
       }

        tCliente.setModel(new DefaultTableModel(tableData, tableHeaders));
      //Ocultando idCliente 
        tCliente.getTableHeader().getColumnModel().getColumn(0).setMaxWidth(0);
        tCliente.getTableHeader().getColumnModel().getColumn(0).setPreferredWidth(0);
        tCliente.getTableHeader().getColumnModel().getColumn(0).setMinWidth(0);
        tCliente.getTableHeader().getColumnModel().getColumn(0).setMaxWidth(0);
        tCliente.getTableHeader().getColumnModel().getColumn(0).setMaxWidth(0);
        
        
        //restringiendo edicion
        
        
    
        } catch (SQLException ex) {
            Logger.getLogger(BusquedaCliente.class.getName()).log(Level.SEVERE, null, ex);
        }
       
      
    }//GEN-LAST:event_tBusquedaKeyReleased

    private void rNombreActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_rNombreActionPerformed
      tBusqueda.setEnabled(true);                        
        if(rNombre.isSelected()){
            rCedula.setSelected(false);
            rDpi.setSelected(false);
            consulta = "WHERE concat(Nombre_cliente,' ', Apellido_cliente) LIKE '%";
        }
        
        
        
        
    }//GEN-LAST:event_rNombreActionPerformed

    private void rCedulaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_rCedulaActionPerformed
            tBusqueda.setEnabled(true);                        
        if(rCedula.isSelected()){
            rNombre.setSelected(false);
            rDpi.setSelected(false);
            consulta = "WHERE concat(No_registro_cliente,No_orden_cliente) LIKE '%";
        }
        
    }//GEN-LAST:event_rCedulaActionPerformed

    private void rDpiActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_rDpiActionPerformed
        tBusqueda.setEnabled(true);                        
        if(rDpi.isSelected()){
            rNombre.setSelected(false);
            rCedula.setSelected(false);
            consulta = "WHERE Dpi_cliente LIKE '%";
        }
    }//GEN-LAST:event_rDpiActionPerformed

    private void BeditarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BeditarActionPerformed
        java.sql.ResultSet res2 =null;
        EditarCliente formEditar = new EditarCliente();
            formEditar.setVisible(true);
         //   EditarCasos editcasos = new EditarCasos();
        try {
            res2 = verCliente.verDatosClienteTabla(tCliente.getValueAt(tCliente.getSelectedRow(),0).toString());
            formEditar.verResultsetObtenerDatosCliente(res2,tCliente.getValueAt(tCliente.getSelectedRow(),0).toString());
            formEditar.LLenarTel();
        } catch (SQLException ex) {
            
        }
    }//GEN-LAST:event_BeditarActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new BusquedaCliente().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton Beditar;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JRadioButton rCedula;
    private javax.swing.JRadioButton rDpi;
    private javax.swing.JRadioButton rNombre;
    private javax.swing.JTextField tBusqueda;
    private javax.swing.JTable tCliente;
    // End of variables declaration//GEN-END:variables
}
